<template>
	<view class="container">
		<z-paging ref="paging" refresher-only @onRefresh="onRefresh">
			<view class="" slot="top">
				<view class="header">
					<TopNav title="商城订单" bg-color="#fff"></TopNav>
					<view class="search bg-white p-tb10 p-lr12">
						<u-search :showAction="true" v-model="queryParams.order_sn" @search="handleSearch"
							@custom="handleSearch" @clear="handleSearch" placeholder="请输入订单编号"
							placeholderColor="#B2B8C1" actionText="搜索" bgColor="#F0F1F2" :animation="true"
							searchIcon="/static/images/mine/search.png" style="font-size: 30rpx;"></u-search>
					</view>
					<view class="tag p-lr12">
						<view class="tag-item" v-for="(item,index) in tagList" :key="index"
							@click="handleTagChange(index,item)" :class="{'tag-active':index === tagCurrent}">
							<text>{{item.text}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="card">
				<view class="card-item" @click="handleToPage(item)" v-for="(item,index) in list" :key="item.id">
					<view class="flex justify-between align-center">
						<view class="flex align-center">
							<text class="text-gray f-s12 f-w400">订单编号：{{item.order_sn}}</text>
						</view>
						<view class="">
							<text class="text-theme f-s12 f-w400">{{item.order_status_text}}</text>
						</view>
					</view>
					<view class="flex full-width m-t10 justify-between">
						<view class="flex flex-1">
							<image class="cover bg-gray" :class="{'m-r4':coverIndex<3}" mode=""
								v-for="(cover,coverIndex) in computeCover(item.goods)" :key="cover"
								:src="cover || '/static/logo.png'"></image>

						</view>

						<view class="m-l8 shrink-0 flex align-center">
							<text class="text-gray f-s12">共{{computeCount(item.goods)}}件</text>
							<image src="/static/images/index/more-1.png" class="width-20 height-20" mode=""></image>
						</view>
					</view>
					<view class="m-t14 flex justify-between p-lr12 p-tb8"
						style="background-color: #F5F6F7;border-radius: 8rpx;">
						<view class="flex align-baseline basis-df">
							<text class="f-s12 text-black">支付方式</text>
							<text class="m-l6 text-theme text-bold f-s14 line-height20">{{item.pay_type_text || '-'}}</text>
						</view>
						<view class="flex align-baseline basis-df">
							<text class="f-s12 text-black">支付金额</text>
							<text class="m-l6 text-theme text-bold f-s14 line-height20">¥{{item.pay_money}}</text>
						</view>
					</view>
					<view class="flex justify-end m-t15" v-if="item.order_status == 0 || item.order_status == 1" @click.stop>
						<view class="card-item-btn shrink-0" @click="handleCancelOrder(item,index)"
							hover-class="pressed">
							<text class="f-s12 f-w500 line-height16">取消</text>
						</view>
					</view>
					<view class="flex justify-end m-t15" v-if="item.order_status == 2" @click.stop>
						<view class="card-item-btn shrink-0" @click="handleOrderReceipt(item,index)"
							hover-class="pressed">
							<text class="f-s12 f-w500 line-height16">确认收货</text>
						</view>
					</view>
				</view>
				<view class="padding-tb-xs" v-if="total !== 0">
					<u-loadmore :status="loadmoreStatus" line @loadmore="handleScrollBottom" />
				</view>
				<Empty :show="total === 0"></Empty>
			</view>
		</z-paging>
		<Modal :show.sync="modalShow" title="提示" content="你确认要取消该商城订单吗" @confirm="handleModalConfirm"></Modal>
			<Toast></Toast>
	</view>
</template>

<script>
	import Empty from '@/components/empty/index.vue';
	import Modal from '@/components/modal/index.vue';
	import mixins from "@/mixins";
	import {
		orderList as list,
		orderCancel,
		orderReceipt
	} from "@/api/mine/shopping.js"
	export default {
		components: {
			Empty,
			Modal
		},
		mixins: [mixins],
		data() {
			return {
				cancelIndex: '',
				cancelParams: {
					order_sn: ''
				},
				modalShow: false,
				searchShow: false,
				tagCurrent: 0,
				list: [],
				queryParams: {
					order_sn: '',
					status: '',
					limit: 10,
					page: 1
				},
				tagList: [{
					text: "全部",
					value: ''
				}, {
					text: "待付款",
					value: '0'
				}, {
					text: "待发货",
					value: '1'
				}, {
					text: "待收货",
					value: '2'
				}, {
					text: "已完成",
					value: '3'
				}, {
					text: "退款",
					value: '4'
				}, {
					text: "取消",
					value: '-1'
				}]
			}
		},
		onShow() {
			if (this.queryParams.page <= 1) {
				this.getList()
			}
		},
		onHide() {
			this.queryParams.order_sn = ''
			this.searchShow = false
		},
		onReachBottom() {
			this.handleScrollBottom()
		},
		methods: {
			findStatusText(status){
				let findResult = this.tagList.find(item=>item.value == status)
				if(status && findResult){
					return findResult['text']
				}else{
					return ''
				}
			},
			handleModalConfirm() {
				this.$modal.loading("正在取消").then(() => {
					orderCancel(this.cancelParams).then(res => {
						this.$set(this.list[this.cancelIndex], 'order_status', -1)
						this.$set(this.list[this.cancelIndex], 'order_status_text', '已取消')
						this.$modal.closeLoading()
						this.$toast(res.msg)
					}).catch(() => this.$modal.closeLoading())
				})

			},
			// 确认收货
			handleOrderReceipt(selected,index){
				this.$modal.loading("正在确认").then(()=>{
					orderReceipt({order_sn:selected.order_sn}).then(res=>{
						if(this.queryParams.status===''){
							this.$set(this.list[index], 'order_status', 3)
							this.$set(this.list[index], 'order_status_text', '已完成')
						}else{
							this.queryParams.page = 1
							this.getList()
						}

						this.$modal.closeLoading()
						this.$toast(res.msg)
					}).catch(() => this.$modal.closeLoading())
				})
			},
			// 取消订单
			handleCancelOrder(selected, index) {
				this.cancelIndex = index
				this.cancelParams.order_sn = selected.order_sn
				this.modalShow = true
			},
			computeCover(list) {
				if (Array.isArray(list) && list.length) {
					if (list.length > 3) list = list.slice(0, 3)
					let coverList = list.map(item => {
						return item.image
					})
					return coverList
				} else {
					return []
				}
			},
			computeCount(list) {
				if (Array.isArray(list) && list.length) {
					let count = list.reduce((prev, curr) => {
						return prev + curr.num || 0
					}, 0)
					return count
				} else {
					return 0
				}
			},
			// 下拉刷新被触发
			onRefresh() {
				this.queryParams.page = 1
				this.getList('scroll')
			},
			handleSearch() {
				this.queryParams.page = 1
				this.getList()
			},
			getList(type) {
				list(this.queryParams).then(res => {
					if (type === 'scroll') {
						this.$refs.paging.complete();
					}
					let {
						data: {
							list,
							count,
							total
						}
					} = res
					this.total = count
					this.pageTotal = total
					if (this.queryParams.page === this.pageTotal) this.loadmoreStatus = 'nomore'
					if (this.queryParams.page === 1) {
						this.list = list
					} else if (this.queryParams.page < this.pageTotal) {
						this.list = this.list.concat(list)
					}
				}).catch(() => {
					if (type === 'scroll') {
						this.$refs.paging.complete();
					}
				})
			},
			handleTagChange(index, select) {
				if (this.tagCurrent === index) return
				this.tagCurrent = index
				this.queryParams.status = select['value']
				this.queryParams.page = 1
				this.getList()
			},
			handleToPage(select) {
				this.$tab.navigateTo('order-detail?id=' + select.order_sn)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: inherit;

		.header {
			background-color: inherit;
			z-index: 99;
			position: sticky;
			top: 0;
			// padding: 0 24rpx 10rpx 24rpx;
			padding-top: calc(var(--status-bar-height));
		}

		.tag {
			width: 100%;
			margin: 24rpx 0;
			display: flex;
			overflow-x: auto;

			.tag-item {
				padding: 16rpx 32rpx;
				background-color: #FFFFFF;
				border-radius: 32rpx;
				font-weight: 400;
				font-size: 24rpx;
				text-align: center;
				margin-right: 20rpx;
				flex-shrink: 0;
			}

			.tag-active {
				background-color: #202020;
				color: #fff;
			}
		}

		.card {
			padding: 0 24rpx 36rpx 24rpx;

			.card-item {
				border-radius: 20rpx;
				padding: 20rpx;
				background-color: #fff;
				margin-bottom: 20rpx;

				.cover {
					width: 176rpx;
					height: 176rpx;
					// background: #D8D8D8;
					border-radius: 16rpx;
					flex-shrink: 0;
				}

				.card-item-tag {
					border-radius: 4rpx;
					border: 1rpx solid #FE3F59;
					font-size: 22rpx;
					color: #FE3F59;
					line-height: 32rpx;
					padding: 0 6rpx;
					margin-right: 20rpx;
				}

				.card-item-btn {
					width: 168rpx;
					height: 72rpx;
					// padding: 6rpx 16rpx;
					background: #FE3F59;
					border-radius: 12rpx;
					font-weight: 400;
					font-size: 22rpx;
					color: #FFFFFF;
					line-height: 72rpx;
					text-align: center;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
				}
			}
		}
	}

	.animated-width {
		width: 0;
		overflow: hidden;
		transition: width 0.5s ease-in-out;
		transform-origin: right;
		/* 设置变换原点为右侧 */
		display: inline-block;
	}

	.animated-width.show {
		width: 100%;
	}
</style>